package familytree;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class Tree {

	// stores the people in the hashmap, name = key, person = value
	private HashMap<String, Person> people = new HashMap<String, Person>();

	public static void main(String[] args) {

		Tree tree = new Tree();
		tree.begin();

	}

	public void begin() {

		String command = "";
		boolean check = true;
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));

		if (check == true) {
			do {
				displaycommands();
				System.out.println("Enter: ");
				try {
				command = reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			String[] input = command.split(" ");
			if (input[0].equals("add")) {
				addPerson(input[1]);
				
			} else if (input[0].equals("print")) {
				printthatshit(people);
				
			
				
			} else if (input[0].equals("help")) {
				help();
			}
			
		} while (!(command.equals("stop")));
		

	}
	}

	private void addPerson(String name) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));

		try {
			System.out.println("Enter your gender ('m' or 'f'):");
			String gender = reader.readLine();
			
			// add the person to the hashmap
			Person person = new Person(gender,name);
			people.put(name, person);
			
			// adds the String value to the father if exists
			System.out.println("Enter your fathers name or leave empty: ");
			String fathername = reader.readLine();
			Person father = people.get(fathername);
			person.setfather(father);

			// adds the String value to the mother if exists
			System.out.println("Enter your mothers name or leave empty: ");
			String mothername = reader.readLine();
			Person mother = people.get(mothername);
			person.setmother(mother);
			
			System.out.println("Enter your childs name or leave empty:");
			String childname = reader.readLine();
			Person child = people.get(childname);
			person.setmother(child);
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	public void printthatshit(HashMap<String, Person> people){
		System.out.println(people);
	}
	
	private void displaycommands() {

		System.out.println("Use one of the following commands: ");
		System.out.println("stop: \t The program stops");
		System.out.println("add: \t Adds a new person to the familytree");
		System.out.println("print: \t prints out the familytree with its members");
	}
	
	private void help() {
		System.out.println("Please use the following commands only:");
		begin();
	}

}